//
//  PersonalSettingViewController.m
//  ChatForHot
//
//  Created by wr on 2018/10/10.
//  Copyright © 2018年 FLY. All rights reserved.
//

#import "PersonalSettingViewController.h"
#import "BaseTableView.h"
#import "PersonalSettingViewModel.h"
#import "PersonalCell.h"
#import "RegisterOrLoginViewController.h"
#import "AppDelegate.h"
#import "PersonalChangePassWordController.h"
#import "PersonalSettingBlockController.h"
#import "PersonalSettingFeedbackController.h"
#import "PersonalSettingPolicyController.h"

@interface PersonalSettingViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) PersonalSettingViewModel *viewModel;
@property (nonatomic, strong) BaseTableView *HOTCHATtableView;
@property (nonatomic, strong) UIButton *HOTCHATbottmToolButton;
@end

@implementation PersonalSettingViewController

- (void)HOTCHATdidInitialized {
    [super HOTCHATdidInitialized];
    self.viewModel = [[PersonalSettingViewModel alloc] init];
}

- (void)HOTCHATInsertSubViews {
    [super HOTCHATInsertSubViews];
    @weakify(self)
    
    self.HOTCHATtableView = [[BaseTableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.HOTCHATtableView.delegate = self;
    self.HOTCHATtableView.dataSource = self;
    self.HOTCHATtableView.tableHeaderView.height = 16;
    [self.HOTCHATtableView registerClass:[PersonalCell class] forCellReuseIdentifier:@"PersonalCell"];
    [self.view addSubview:self.HOTCHATtableView];
    
    self.HOTCHATbottmToolButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.HOTCHATbottmToolButton setTitle:NSLocalizedString(@"personS5", nil) forState:(UIControlStateNormal)];
    [self.HOTCHATbottmToolButton setTitleColor:[UIColor colorFromHexString:@"#F04D32"] forState:UIControlStateNormal];
    [self.HOTCHATbottmToolButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [self.HOTCHATbottmToolButton setBackgroundImage:[[UIImage imageNamed:@"personal_btn_del"] resizableImageWithCapInsets:UIEdgeInsetsMake(15, 15, 15, 15) resizingMode:(UIImageResizingModeStretch)] forState:UIControlStateNormal];
    [self.HOTCHATbottmToolButton setBackgroundImage:[[UIImage imageNamed:@"personal_btn_del_hig"] resizableImageWithCapInsets:UIEdgeInsetsMake(15, 15, 15, 15) resizingMode:(UIImageResizingModeStretch)] forState:UIControlStateHighlighted];
    
    [[self.HOTCHATbottmToolButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        @strongify(self)
        [self HOTCHATshowlogoutAlert];
    }];
    
    [self.viewModel.requestLogoutCommand.executionSignals subscribeNext:^(RACSignal *excution) {
        [[excution dematerialize] subscribeNext:^(id x) {
            [self HOTCHATstopLoading];
            RegisterOrLoginViewController *registerLoginvc = [[RegisterOrLoginViewController alloc]init];
            UINavigationController *nv = [[UINavigationController alloc] initWithRootViewController:registerLoginvc];
            AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
            appdelegate.window.rootViewController = nv;
            [appdelegate.window makeKeyAndVisible];
        } error:^(NSError *error) {
        }];
    }];
    
    [self.view addSubview:self.HOTCHATbottmToolButton];
}

- (void)HOTCHATsetNavigationItemsIsInEditMode:(BOOL)isInEditMode animated:(BOOL)animated {
    [super HOTCHATsetNavigationItemsIsInEditMode:isInEditMode animated:animated];
    self.title = NSLocalizedString(@"person10", nil);
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.HOTCHATtableView.frame = CGRectMake(0, 0,
                                      kScreenWidth,
                                      kScreenHeight - ((iPhoneX || iPhoneXR || iPhoneXSMAX) ? 44 : 20) - 44);
    self.HOTCHATbottmToolButton.size = CGSizeMake(kScreenWidth - 30, 44);
    self.HOTCHATbottmToolButton.left = 15;
    self.HOTCHATbottmToolButton.bottom = self.HOTCHATtableView.bottom - 15;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    @weakify(self)
    [self.viewModel.changePasswordDidSelectSubject subscribeNext:^(id x) {
        @strongify(self)
        PersonalChangePassWordController *vc = [[PersonalChangePassWordController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }];
    
    [self.viewModel.blockDidSelectSubject subscribeNext:^(id x) {
        @strongify(self)
        PersonalSettingBlockController *vc = [[PersonalSettingBlockController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }];
    
    [self.viewModel.suggesstionDidSelectSubject subscribeNext:^(id x) {
        @strongify(self)
        PersonalSettingFeedbackController *vc = [[PersonalSettingFeedbackController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }];
    
    [self.viewModel.policyDidSelectSubject subscribeNext:^(id x) {
        PersonalSettingPolicyController *vc = [[PersonalSettingPolicyController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }];
}

#pragma mark - <UITableViewDataSource,UITableViewDelegate>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.viewModel.cellDataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    PersonalCellModel *row = [self.viewModel.cellDataSource objectOrNilAtIndex:indexPath.row];
    return row.cellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PersonalCellModel *cellModel = [self.viewModel.cellDataSource objectOrNilAtIndex:indexPath.row];
    PersonalCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PersonalCell"];
    [cell HOTCHATbind:cellModel];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PersonalCellModel *cellModel = [self.viewModel.cellDataSource objectOrNilAtIndex:indexPath.row];
    if (cellModel.handler) { cellModel.handler(); }
    
}

- (void)HOTCHATshowlogoutAlert{
    @weakify(self)
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"personS5", nil) message:nil preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *a1 = [UIAlertAction actionWithTitle:NSLocalizedString(@"OKBUTTON", nil) style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        @strongify(self)
       
        NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
        [dic setValue:[APPINFO defaultManager].accountid forKey:@"accountid"];
        [dic setValue:[APPINFO defaultManager].accountNumber forKey:@"accountNumber"];
        [self HOTCHATstartLoading];
        [self.viewModel.requestLogoutCommand execute:dic];
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:NSLocalizedString(@"CANCELBUTTON", nil) style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alert addAction:cancel];
    [alert addAction:a1];
    [self presentViewController:alert animated:YES completion:^{
    }];
}
@end
